﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, Procedure SelectEntity

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO

create table #TestCases (
	TestNo              integer not null           identity (1, 1),
	BaseEntityName      nvarchar(512),
	EntityName          nvarchar(512),
	EntityType          varchar(128),
	Level0Name          nvarchar(128),
	Level0Type          varchar(64),
	Level1Name          nvarchar(128),
	Level1Type          varchar(64),
	Level2Name          nvarchar(128),
	Level2Type          varchar(64),
	ErrorCode           integer,
	primary key (TestNo)
);
GO

insert into #TestCases
values
	(null, null, 'DATABASE', null, null, null, null, null, null, null),
	(null, 'TestSchema', 'DATABASE', null, null, null, null, null, null, 740900),
	(null, '.', '', null, null, null, null, null, null, null),
	(null, '.', null, null, null, null, null, null, null, null),
	('TestSchema', '.', 'SCHEMA', null, null, null, null, null, null, 740900),
	('TestSchema', '.', 'DATABASE', null, null, null, null, null, null, null),
	('TestSchema', '.TestTable', 'TABLE', 'TestSchema', 'SCHEMA', 'TestTable', 'TABLE', null, null, null),
	('TestSchema', 'sys', 'SCHEMA', 'sys', 'SCHEMA', null, null, null, null, null),
	('TestSchema', 'sys', null, 'sys', 'SCHEMA', null, null, null, null, null),
	('Some Name', null, 'DATABASE', null, null, null, null, null, null, null);
GO

declare @TestNo as integer;
declare @BaseEntityName as nvarchar(512);
declare @EntityName as nvarchar(512);
declare @EntityType as varchar(128);
declare @Level0Name as nvarchar(128);
declare @Level0Type as varchar(64);
declare @Level1Name as nvarchar(128);
declare @Level1Type as varchar(64);
declare @Level2Name as nvarchar(128);
declare @Level2Type as varchar(64);
declare @ErrorCode as integer;

set @TestNo = (select min(TestNo) from #TestCases);

while
	@TestNo is not null
begin
	
	-- Fetch input data
	set @BaseEntityName = (select BaseEntityName from #TestCases where TestNo = @TestNo);
	set @EntityName = (select EntityName from #TestCases where TestNo = @TestNo);
	set @EntityType = (select EntityType from #TestCases where TestNo = @TestNo);
	
	-- Reset output variables
	set @Level0Name = null;
	set @Level0Type = null;
	set @Level1Name = null;
	set @Level1Type = null;
	set @Level2Name = null;
	set @Level2Type = null;
	set @ErrorCode = null;
	
	-- Set base entity in temp table
	execute VanguardDocumentation.SetBaseEntity @BaseEntityName;
	
	begin try;
		execute VanguardDocumentation.SelectEntity
			@EntityName, @EntityType, @Level0Name out, @Level0Type out,
			@Level1Name out, @Level1Type out, @Level2Name out, @Level2Type out;
	end try
	begin catch
		set @ErrorCode = error_number();
	end catch;

	-- No error and difference in output
	if
		exists (
			select TestNo, BaseEntityName, EntityName, EntityType,
				Level0Name, Level0Type, Level1Name, Level1Type, Level2Name, Level2Type
			from #TestCases
			where TestNo = @TestNo and ErrorCode is null
			except
			select @TestNo, @BaseEntityName, @EntityName, @EntityType,
				@Level0Name, @Level0Type, @Level1Name, @Level1Type, @Level2Name, @Level2Type
			where @ErrorCode is null
		)
	begin
		print 'SelectEntity, Failed test no ' + rtrim(@TestNo);
		print '	(BaseEntity ' + coalesce(quotename(@BaseEntityName), 'null')
			+ ') ' + coalesce(quotename(@EntityName, ''''), 'null')
			+ ', ' + coalesce(quotename(@EntityType, ''''), 'null')
			+ ' >>> ' + coalesce(quotename(@Level0Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level0Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Type, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end
	-- Difference in errors (ignores differences in output for errors)
	else if
		exists (
			select TestNo, BaseEntityName, EntityName, EntityType, ErrorCode
			from #TestCases where TestNo = @TestNo
			except
			select @TestNo, @BaseEntityName, @EntityName, @EntityType, @ErrorCode
		)
	begin
		print 'SelectEntity, Failed test no ' + rtrim(@TestNo);
		print '	(BaseEntity ' + coalesce(quotename(@BaseEntityName), 'null')
			+ ') ' + coalesce(quotename(@EntityName, ''''), 'null')
			+ ', ' + coalesce(quotename(@EntityType, ''''), 'null')
			+ ' >>> ' + coalesce(quotename(@Level0Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level0Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Type, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end;
	

	set @TestNo = (select min(TestNo) from #TestCases where TestNo > @TestNo);

end;
GO

if
	object_id('tempdb.dbo.#VanguardDocumentationBaseEntity') is not null
begin
	drop table #VanguardDocumentationBaseEntity;
end;
GO

drop table #TestCases;
GO

set nocount off;
GO

/*
-- Checking a specific test

create table #VanguardDocumentationBaseEntity (
	Name           nvarchar(512)
);
GO

insert into #VanguardDocumentationBaseEntity
values ('TestSchema');
GO

declare @Level0Name as nvarchar(128);
declare @Level0Type as varchar(64);
declare @Level1Name as nvarchar(128);
declare @Level1Type as varchar(64);
declare @Level2Name as nvarchar(128);
declare @Level2Type as varchar(64);

execute VanguardDocumentation.SelectEntity '.TestTable', 'TABLE',
	@Level0Name out, @Level0Type out, @Level1Name out, @Level1Type out, @Level2Name out, @Level2Type out;

select @Level0Name as Level0Name, @Level0Type as Level0Type, @Level1Name as Level1Name, @Level1Type as Level1Type,
	@Level2Name as Level2Name, @Level2Type as Level2Type;
GO

drop table #VanguardDocumentationBaseEntity;
GO

*/




